package com.dou.sample.opengl_project;

/**
 * Created by Dou on 2018/1/15.
 */

public class Plane extends Mesh {

    public Plane() {
        this(1, 1, 1, 1);
    }

    public Plane(float width, float height) {
        this(width, height, 1, 1);
    }

    public Plane(float width, float height, int widthSegment, int heightSegment) {
        float[] vertexs = new float[(widthSegment + 1) * (heightSegment + 1) * 3];
        short[] indices = new short[(widthSegment + 1) * (heightSegment + 1) * 6];

        float offsetX = width / -2;
        float offsetY = height / -2;

        float xWidth = width / widthSegment;
        float yHeight = height / heightSegment;

        int currentVertex = 0;
        int currentIndex = 0;

        short w = (short) (widthSegment + 1);

        for (int y = 0; y < heightSegment + 1; y++) {
            for (int x = 0; x < widthSegment + 1; x++) {
                vertexs[currentVertex] = offsetX + x * xWidth;
                vertexs[currentVertex + 1] = offsetY + y * yHeight;
                vertexs[currentVertex + 2] = 0;
                currentVertex += 3;

                int n = (widthSegment + 1) * y + x;

                // face one
                if (y < heightSegment && x < widthSegment) {
                    indices[currentIndex] = (short) n;
                    indices[currentIndex + 1] = (short) (n + 1);
                    indices[currentIndex + 2] = (short) (n + w);

                    indices[currentIndex + 3] = (short) (n + 1);
                    indices[currentIndex + 4] = (short) (n + 1 + w);
                    indices[currentIndex + 5] = (short) (n + 1 + w - 1);

                    currentIndex += 6;
                }
            }
        }

        setVertex(vertexs);
        setIndices(indices);
    }
}
